package com.graduation.sports.ui.activity;

import android.content.ContentValues;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.graduation.sports.R;
import com.graduation.sports.base.BaseActivity;
import com.graduation.sports.bean.SportDiaryBean;
import com.graduation.sports.config.Session;
import com.graduation.sports.util.DateUtils;
import com.graduation.sports.util.UIHelper;
import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.CalendarLayout;
import com.haibin.calendarview.CalendarView;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;

import org.litepal.LitePal;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class DiaryActivity extends BaseActivity {
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.diary_add)
    ImageView diaryAdd;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.diary_content)
    TextView tvDiaryContent;
    @BindView(R.id.calendarView)
    CalendarView mCalendarView;
    @BindView(R.id.calendarLayout)
    CalendarLayout mCalendarLayout;

    private String queryDate = "";
    private boolean isData = false;
    private SportDiaryBean diaryBean = null;


    @Override
    protected int getLayout() {
        return R.layout.activity_diary;
    }

    @Override
    protected void initDataAndEvent(Bundle savedInstanceState) {
        tvTitle.setText("运动日记");
        queryDate = DateUtils.getOldDate(0);

        if (LitePal.isExist(SportDiaryBean.class, "date = ? and userAccount = ?", queryDate, Session.getUserInfo().getUserAccount())) {
            diaryBean = LitePal.where("date = ? and userAccount = ?", queryDate, Session.getUserInfo().getUserAccount()).find(SportDiaryBean.class).get(0);
            diaryAdd.setVisibility(View.GONE);
            isData = true;
        }
        refreshDiary();
        initListener();
    }

    @OnClick({R.id.img_back,R.id.diary_add,R.id.diary_content})
    public void onViewClick(View view){
        int id = view.getId();
        if (id == R.id.img_back) {
            finish();
        }
        if (id == R.id.diary_add) {
            showAddDiaryDialog();
        }
        if (id == R.id.diary_content) {
            if (isData) {
                showAddDiaryDialog();
            }

        }

    }


    private void showAddDiaryDialog(){
        QMUIDialog.CustomDialogBuilder builder = new QMUIDialog.CustomDialogBuilder(this);
        QMUIDialog diaryDialog = builder.setLayout(R.layout.layout_diary_add_dialog)
                .setTitle("运动日记")
                .addAction(0,"取消", (dialog, index) -> dialog.dismiss())
                .addAction(0, "确认", (dialog, index) -> {
                    EditText task=dialog.findViewById(R.id.diary_content);
                    Switch aSwitch = dialog.findViewById(R.id.switch_isopen);
                    String content = task.getText().toString().trim();
                    final int[] isOpen = {0};
                    if (TextUtils.isEmpty(content)) {
                        ToastUtils.showShort("请输入内容！");
                        return;
                    }

                    aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                            isOpen[0] = isChecked ? 1 : 0;
                        }
                    });
                    SportDiaryBean  diaryBean = new SportDiaryBean();
                    diaryBean.setContent(content);
                    diaryBean.setDate(DateUtils.getOldDate(0));
                    diaryBean.setIsOpen(isOpen[0]);
                    diaryBean.setUserAccount(Session.getUserInfo().getUserAccount());

                    if (isData) {
                        ContentValues values = new ContentValues();
                        values.put("content",diaryBean.getContent());
                        values.put("isOpen",diaryBean.getIsOpen());
                        int result = LitePal.updateAll(SportDiaryBean.class, values, "date = ? and userAccount = ? ",
                                queryDate, Session.getUserInfo().getUserAccount());
                        if (result > 0) {
                            dialog.dismiss();
                            refreshDiary();
                        } else {
                            ToastUtils.showShort("日记发布失败！");
                            return;
                        }
                    } else {
                        if (diaryBean.save()) {
                            dialog.dismiss();
                            refreshDiary();
                        } else {
                            ToastUtils.showShort("日记发布失败！");
                            return;
                        }
                    }

                }).create(R.style.DialogThemeDiary);
        diaryDialog.show();
        if (isData) {
            EditText editText = diaryDialog.findViewById(R.id.diary_content);
            Switch isOpen = diaryDialog.findViewById(R.id.switch_isopen);
            editText.setText(diaryBean.getContent());
            isOpen.setChecked(diaryBean.getIsOpen() == 1);
        }
    }

    private void refreshDiary(){
        List<SportDiaryBean> diaryBeans = LitePal.where("date = ? and userAccount = ?", queryDate, Session.getUserInfo().getUserAccount()).find(SportDiaryBean.class);
        if (diaryBeans.size() <= 0) {
            tvDiaryContent.setText("暂无日记");
            return;
        } else {
            tvDiaryContent.setText(diaryBeans.get(0).getContent());
        }
    }

    private void initListener() {
        mCalendarView.setOnCalendarSelectListener(new CalendarView.OnCalendarSelectListener() {
            @Override
            public void onCalendarOutOfRange(Calendar calendar) {

            }

            @Override
            public void onCalendarSelect(Calendar calendar, boolean isClick) {
                queryDate = DateUtils.formatStringDateShort(calendar.getYear(), calendar.getMonth(), calendar.getDay());
                refreshDiary();
            }
        });
    }
}
